using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cou;
using net_work.IService.Cou;

namespace net_work.Controllers.Cou;

[Route("api/[controller]")]
[ApiController]
public class TeacherTeachingController(ILogger<TeacherTeachingController> logger, ITeacherTeachingService teacherTeachingService) : ControllerBase
{
    /// <summary>
    /// 获取教师授课列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListTeacherTeachings([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取教师授课列表");
        var result = await teacherTeachingService.GetTeacherTeachingsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取教师授课信息
    /// </summary>
    /// <param name="teacherTeachingId">教师授课ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetTeacherTeachingById([FromQuery]long teacherTeachingId)
    {
        logger.LogInformation("获取教师授课信息");
        return await teacherTeachingService.SelectByIdAsync(teacherTeachingId) is { } teacherTeaching
            ? AjaxResult.Success(teacherTeaching)
            : AjaxResult.Error();
    }
    
    /// <summary>
    /// 新增教师授课
    /// </summary>
    /// <param name="teacherTeachingAddRequest">新增教师授课实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddTeacherTeaching([FromBody] TeacherTeachingAddRequest teacherTeachingAddRequest)
    {
        logger.LogInformation("新增教师授课");
        var teacherTeaching = await teacherTeachingService.InsertTeacherTeaching(teacherTeachingAddRequest);
        return teacherTeaching > 0
            ? AjaxResult.Success(new { teacherTeaching })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改教师授课
    /// </summary>
    /// <param name="teacherTeachingUpdateRequest">修改教师授课实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateTeacherTeaching([FromBody] TeacherTeachingUpdateRequest teacherTeachingUpdateRequest)
    {
        logger.LogInformation("修改教师授课");
        return await teacherTeachingService.UpdateTeacherTeaching(teacherTeachingUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除教师授课
    /// </summary>
    /// <param name="ids">教师授课ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteTeacherTeaching([FromBody] List<long> ids)
    {
        logger.LogInformation("删除教师授课");
        return await teacherTeachingService.DeleteTeacherTeachingByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}